﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;

public static class UnityEditorHelper  {

    public static void DrawFieldOnEditorGUI(FieldInfo field,object target)
    {
        EditorGUILayout.BeginHorizontal();
        if (field.FieldType.IsClass)
        {
            EditorGUILayout.LabelField(field.Name);
            field.SetValue(target, EditorGUILayout.ObjectField(field.GetValue(target) as UnityEngine.Object, field.GetType(), true));
        }
        else
        {
            if (field.FieldType == typeof(float))
            {
                EditorGUILayout.LabelField(field.Name);
                field.SetValue(target, EditorGUILayout.FloatField((float)field.GetValue(target)));
            }
            else if (field.FieldType == typeof(int))
            {
                EditorGUILayout.LabelField(field.Name);
                field.SetValue(target, EditorGUILayout.IntField((int)field.GetValue(target)));
            }
            else if (field.FieldType == typeof(bool))
            {
                EditorGUILayout.LabelField(field.Name);
                field.SetValue(target, EditorGUILayout.Toggle((bool)field.GetValue(target)));
            }
            else if(field.FieldType.IsEnum)
            {
                EditorGUILayout.LabelField(field.Name);
                bool isFlags =  field.GetCustomAttributes(typeof(System.FlagsAttribute), true).Length>0;
                if(isFlags)
                {
                    field.SetValue(target, EditorGUILayout.EnumMaskField((System.Enum)field.GetValue(target)));
                }
                else
                {
                    field.SetValue(target, EditorGUILayout.EnumPopup((System.Enum)field.GetValue(target)));
                }
            }
            else if(field.FieldType == typeof(Vector3)) field.SetValue(target, EditorGUILayout.Vector3Field(field.Name, (Vector3)field.GetValue(target)));
            else if (field.FieldType == typeof(Vector2)) field.SetValue(target, EditorGUILayout.Vector2Field(field.Name, (Vector2)field.GetValue(target)));
            else EditorGUILayout.LabelField("不支持的类型:"+field.FieldType.ToString());
        }
        EditorGUILayout.EndHorizontal();
    }

}
